<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录 - 慰风博客</title>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        [v-cloak] {
            display: none;
        }
        body {
            margin: 0;
            padding: 0;
            background-color: #f5f7fa;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }
        .login-container {
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
        }
        .login-title {
            text-align: center;
            margin-bottom: 30px;
            font-size: 24px;
            color: #333;
        }
        .login-form {
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
        }
        .login-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div id="app" v-cloak>
        <div class="login-container">
            <h1 class="login-title">慰风博客</h1>
            <div class="login-form">
                <el-form ref="loginForm" :model="loginForm" :rules="rules">
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" placeholder="用户名">
                            <template slot="prepend">
                                <i class="el-icon-user"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="loginForm.password" type="password" placeholder="密码">
                            <template slot="prepend">
                                <i class="el-icon-lock"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                    <div class="login-actions">
                        <el-button type="text" @click="goToRegister">注册账号</el-button>
                        <el-button type="primary" @click="handleLogin" :loading="loading">登录</el-button>
                    </div>
                </el-form>
            </div>
        </div>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    loginForm: {
                        username: '',
                        password: ''
                    },
                    rules: {
                        username: [
                            { required: true, message: '请输入用户名', trigger: 'blur' }
                        ],
                        password: [
                            { required: true, message: '请输入密码', trigger: 'blur' }
                        ]
                    },
                    loading: false
                }
            },
            methods: {
                async handleLogin() {
                    try {
                        await this.$refs.loginForm.validate();
                        this.loading = true;
                        const res = await axios.post('/api/users/login', this.loginForm);
                        if (res.data.code === 200) {
                            localStorage.setItem('token', res.data.data.token);
                            this.$message.success('登录成功');
                            window.location.href = '/';
                        } else {
                            this.$message.error(res.data.message || '登录失败');
                        }
                    } catch (error) {
                        if (error.response) {
                            this.$message.error(error.response.data.message || '登录失败');
                        } else {
                            this.$message.error('登录失败');
                        }
                        console.error('登录失败:', error);
                    } finally {
                        this.loading = false;
                    }
                },
                goToRegister() {
                    window.location.href = '/register.html';
                }
            }
        });
    </script>
</body>
</html> 